<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

if (empty($this->Param['option'])) {    
    //订单列表
    $table = 'order_oldinfo';
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        if($ajax==1){
            $where = array(
                //'`o_status` NOT IN (?)' => array('征信提交')
                '`o_app_status` NOT IN (?)' => array('')
            );
        }else{
            if($ajax ==2){
                $where = array(
                    //'`o_status` NOT IN (?)' => array('征信提交')
                    '`o_app_status` NOT IN (?)' => array('征信提交', '征信查询', '作废', '退回'),
                    '`o_car_pose` = ? AND o_carcard_img IS NULL AND o_2check_date IS NULL AND o_ad_money IS NULL '=>'二手车',
                );
            }
            if($ajax==3){
                $where = array(
                    //'`o_status` NOT IN (?)' => array('征信提交')
                    '`o_app_status` NOT IN (?)' => array('征信提交', '征信查询', '作废', '退回'),
                    '`o_car_pose` = ? AND o_carcard_img IS NOT NULL AND o_2check_date IS NULL '=>'二手车',
                );
            }
            if($ajax ==4){
                $where =array(
                    '`o_ad_money` > ?' => 0,
                    '`o_image_status` IN (?) AND (o_cprice_date IS NULL OR o_getcar_date IS NULL)' => array('退回','未提交')
                );
                }
        }
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,$table);
        //$json_data = array('total' => $data['count'], 'rows' => $data['all']);
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
    } else {        
        echo $this->GetView('supplement_index.php');
    }
} else {
    switch ($this->Param['option']) {
        case 'info':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) ErrorMsg::Debug('无权操作');
            $order = $OrderHelper->orderInfo($oid);
            if(!empty($order['car']['ot_carcard_img'])){
                $mode=array('car_image');
                $order['car']['attachment']=$OrderHelper->etattachmentOut($order['car']['ot_carcard_img'],$mode);
            }
            if(!empty($order['car']['ot_carfare_img'])){
                $mode1=array('carfare_image');
                $order['car']['carfare_img'] = $OrderHelper->etattachmentOut($order['car']['ot_carfare_img'],$mode1);
            }
            if(!empty($order['car']['ot_getcar_img'])){
                $mode1=array('getcar_image');
                $order['car']['getcar_img'] = $OrderHelper->etattachmentOut($order['car']['ot_getcar_img'],$mode1);
            }
            //是否显示订单修改保存按钮 1表示已放款
            $result = $this->LoadModel('Public')->getOneRow('order_oldinfo','*',array('`o_id` = ?' => $oid));
            $msg = $OrderHelper->getAudit(array('`a_type`=?'=>'A00002终审审核','`a_oid` =?' =>$oid,'`a_details`=?' => '银行面签审核通过'));
            $order['video_audit'] = empty($msg)? 0 : 1;
            $order['paidmoney'] = $result['o_ad_money'] > 0 ? 1 : 0; 
            $this->Assign('order', $order);
            echo $this->GetView('supplement_info.php');
            break;
        case 'save':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) ErrorMsg::Debug('无权操作');
            $order_info = $OrderHelper->orderRow($oid);
            if (empty($order_info)) ErrorMsg::Debug('订单不存在');

            $data = $this->Param;
            $check_pay = $this->LoadModel('Public')->getOneRow('order_oldinfo','*',array('`o_id` = ?' => $oid));
            $check_ad = $this->LoadModel('Public')->getOneRow('order_car','*',array('`ot_oid` = ?' => $oid));
            $check_data=unserialize($check_ad['ot_calculated']);
            $d_signag_money=$check_data['d_signag_money'];

            if(isset($data['ot_calculated']['d_signag_money'])) {//如果修改申请分期付款总额
                if($check_pay['o_ad_money']>0)//如果已垫款
                    if ($d_signag_money != $data['ot_calculated']['d_signag_money'])//如果已垫款的情况下，影响了申请分期付款总额拒绝修改
                        ErrorMsg::Debug('已垫款,无法保存修改');
            }

            if(!empty($data['ot_carno'])){
                if(strlen($data['ot_carno']) != 17){
                    ErrorMsg::Debug('请正确填写车架号');
                }
            }
            if(!empty($data['ot_car_register'])){
                if(strlen($data['ot_car_register']) != 12 || !(is_numeric($data['ot_car_register']))){
                    ErrorMsg::Debug('请正确填写机动车登记证书号');
                }
            }
            if (isset($data['ot_loan_type'])) {
                $rate_check = $BankHelper->rateCheck($data);
                if (!$rate_check['success']) {
                    ErrorMsg::Debug($rate_check['msg']);
                }
                $data['ot_bankrt_rate'] = $data['ot_calculated']['bank_rate'] * 100;
            }
            if (isset($data['ot_assessment']) || isset($data['ot_sinocap']) || isset($data['ot_estimate'])) {
                if ($member_info['u_dp_type'] != 'dp' && !in_array($order_info['o_status'], array('征信提交', '征信查询'))) {
                    ErrorMsg::Debug('非部门用户无法保存非征信提交及征信查询订单');
                }
            }
            if (isset($data['ot_calculated'])) {
                $data['ot_calculated'] = serialize($data['ot_calculated']);
            }

            if($this->Param['type']=='etcar'){                
                if(count($data['car_image'])>0){
                    $data['car_image'] = array_slice($data['car_image'],0,8);
                    $data['ot_carcard_img'] = serialize(array('car_image'=>$data['car_image']));
                }else{
                    $data['ot_carcard_img'] = '';
                }
                $data['ot_forecat_time']=date('Y-m-d',time());
            }
            if($this->Param['type']=='carfare'){
                if(count($data['carfare_image'])>0){
                    $data['ot_carfare_img'] = serialize(array('carfare_image'=>$data['carfare_image']));
                }
                else{
                    $data['ot_carfare_img'] = '';
                }
            }
            if($this->Param['type'] == 'getcar'){
                if(count($data['getcar_image'])>0){
                    $data['ot_getcar_img'] = serialize(array('getcar_image'=>$data['getcar_image']));
                }else{
                    $data['ot_getcar_img'] = '';
                }
            }
            if($this->Param['type'] == 'submit'){
                $do =$OrderHelper->carimgSubmit($oid,$member_info['u_realname']);
                if($do['succ']){
                    ErrorMsg::Debug('提交成功',TRUE);
                }else{
                    ErrorMsg::Debug($do['msg']);
                }
            }

            if(isset($data['car_sure'])){
                $data['ot_2check_user']=$member_info['u_realname'];
                $data['ot_2check_date']=date('Y-m-d',time());
            }
            $result = $OrderHelper->carUpdate($oid, $data);
            if ($result['success']) {
                $MemberHelper->saveLog($this->UserConfig, '车辆信息补录', json_encode($this->Param), $this->Controller . '_' . $this->Action,$oid);
                //$OrderHelper->attachCheck($oid);
                if($check_pay['o_lrid']>0 && !($check_pay['o_ad_money'] > 0)){
                    ErrorMsg::Debug('信息保存成功', TRUE); //未垫款
                }else{
                    ErrorMsg::Debug('保存成功'.$result['msg'], TRUE);
                }
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'skip_twice'://跳过二次征信
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $ct_id = empty($this->Param['ct_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['ct_id'];
            $skip = empty($this->Param['skip']) ? 0 : 1;
            $cert_row = $OrderHelper->certRow(array('`ct_oid` = ?' => $oid, '`ct_id` = ?' => $ct_id, '`ct_status` != ?' => 'del'));
            if (!empty($cert_row)) {
                $do = $OrderHelper->certSave(array('ct_skip' => $skip), $cert_row['ct_id']);
                if ($do) {
                    ErrorMsg::Debug('处理成功', TRUE);
                } else {
                    ErrorMsg::Debug('处理失败');
                }
            } else {
                ErrorMsg::Debug('未找到相关订单主贷信息');
            }
            break;
        case 'troremove':  //作废订单
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if (!$order_check['success']) ErrorMsg::Debug('无权操作');
            $result = $OrderHelper->orderInvalid($oid, true);
            if ($result['success']) {
                $MemberHelper->saveLog($this->UserConfig, '作废客户', json_encode($this->Param), $this->Controller . '_' . $this->Action, $oid);
                ErrorMsg::Debug('处理成功', TRUE);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'gpsget'://获取状态
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $order_car = $OrderHelper->orderCar($oid);
            if (empty($order_car)) ErrorMsg::Debug('订单不存在');
            if ($order_car['ot_gps_status']) {
                ErrorMsg::Debug($order_car['ot_gps_status']);
            } else {
                if ($order_car['ot_gps_no']) {
                    $r1 = \yii2api\classes\yii2api\Gps::getGps($order_car['ot_gps_no'], 0, serialize(array('o_add_time' => $order_car['ot_dateline'] - 864000)));
                    if ($r1->data->other_data->gps_status == '已上线') {
                        $data['ot_gps_status'] = '已上线';
                        $data['ot_gps_otime'] = empty($r1->data->other_data->gps_online_time)? date("Y-m-d", NOW_TIME) :date("Y-m-d", $r1->data->other_data->gps_online_time);
                        $result = $OrderHelper->carUpdate($oid, $data);
                        ErrorMsg::Debug('已上线，上线时间为：' . $data['ot_gps_otime'], TRUE);
                    } else {
                        if ($r1->data->other_data->r_gpstime) {
                            ErrorMsg::Debug('查得此GPS上线时间为'.$r1->data->other_data->r_gpstime.',超出判定时间！');
                        } else {
                            ErrorMsg::Debug('未查得此GPS上线信息！');
                        }
                    }
                } else {
                    ErrorMsg::Debug('GPS序列号尚未录入，请确认是否已保存GPS信息');
                }
            }
            break;
        case 'gpsgetall':
            $OrderHelper->carGetgps();
            break;
        case 'gpsclean':  //冲突订单GPS信息清除
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) ErrorMsg::Debug('无权操作');
            $result = $OrderHelper->gpsClean($oid);
            if ($result['success']){
                ErrorMsg::Debug('处理成功', TRUE);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'pay_again': //再次垫款
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $this->LoadModel('Public')->update('order',array('o_needpay'=>1),array('`o_id` = ?' => $oid));
            ErrorMsg::Debug('再次垫款成功');
            break;
        default :
    }
}